import UIKit

class SegmentedControl: UIViewController {
    
    // MARK: - 控件属性
    private let segmentedControl = UISegmentedControl()
    private let contentLabel = UILabel()
    private let resetButton = UIButton(type: .system)
    
    // 分段选项
    private let segmentTitles = ["首页", "发现", "消息", "我的"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UISegmentedControl
        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
        segmentedControl.insertSegment(withTitle: segmentTitles[0], at: 0, animated: false)
        segmentedControl.insertSegment(withTitle: segmentTitles[1], at: 1, animated: false)
        segmentedControl.insertSegment(withTitle: segmentTitles[2], at: 2, animated: false)
        segmentedControl.insertSegment(withTitle: segmentTitles[3], at: 3, animated: false)
        segmentedControl.selectedSegmentIndex = 0  // 默认选中第一个
        segmentedControl.backgroundColor = .systemGray6
        segmentedControl.selectedSegmentTintColor = .systemBlue  // 选中项背景色
        //[.foregroundColor: UIColor.white] 是一个字典，其中 .foregroundColor 是 NSAttributedString.Key 类型的键，表示文本颜色。
        segmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        //当分段控件中的某个选项未被选中时（即正常状态），该选项的文本颜色将使用系统默认的文本颜色（UIColor.label）。
        segmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.label], for: .normal)
        segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)
        view.addSubview(segmentedControl)
        
        // 创建内容标签
        contentLabel.translatesAutoresizingMaskIntoConstraints = false
        contentLabel.font = UIFont.systemFont(ofSize: 24, weight: .medium)
        contentLabel.textAlignment = .center
        contentLabel.numberOfLines = 0
        contentLabel.text = "当前选中：\(segmentTitles[0])"
        view.addSubview(contentLabel)
        
        // 创建重置按钮
        resetButton.translatesAutoresizingMaskIntoConstraints = false
        resetButton.setTitle("重置选择", for: .normal)
        resetButton.backgroundColor = .systemBlue
        resetButton.tintColor = .white
        resetButton.layer.cornerRadius = 8
        resetButton.addTarget(self, action: #selector(resetSegmentedControl), for: .touchUpInside)
        view.addSubview(resetButton)
    }
    
    // MARK: - 布局约束
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // SegmentedControl 约束
            segmentedControl.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin),
            segmentedControl.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin),
            segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: margin * 2),
            
            // 内容标签约束
            contentLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin * 2),
            contentLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin * 2),
            contentLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            
            // 重置按钮约束
            resetButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            resetButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -margin * 3),
            resetButton.widthAnchor.constraint(equalToConstant: 150),
            resetButton.heightAnchor.constraint(equalToConstant: 44)
        ])
    }
    
    // MARK: - 交互逻辑
    @objc private func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        let selectedIndex = sender.selectedSegmentIndex
        let selectedTitle = segmentTitles[selectedIndex]
        contentLabel.text = "当前选中：\(selectedTitle)"
        
        // 根据选择执行不同操作
        performActionForSegment(selectedIndex)
    }
    
    @objc private func resetSegmentedControl() {
        // 重置为第一个选项
        segmentedControl.selectedSegmentIndex = 0
        segmentedControlValueChanged(segmentedControl)
    }
    
    private func performActionForSegment(_ index: Int) {
        // 根据不同的分段索引执行不同操作
        switch index {
        case 0:
            // 首页逻辑
            contentLabel.textColor = .label
        case 1:
            // 发现逻辑
            contentLabel.textColor = .systemBlue
        case 2:
            // 消息逻辑
            contentLabel.textColor = .systemRed
        case 3:
            // 我的逻辑
            contentLabel.textColor = .systemGreen
        default:
            break
        }
    }
}
